package dk.marten.ibdrecord;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class HistoryListAdaptor<E> extends ArrayAdapter<E> {

	private final int layoutResourceId;
	private final int timeResId;
	private final int dateResId;
	private final List<E> tempObjects;
	
	 static class ViewHolder {
	    public TextView time;
	    public TextView date;
	  }

	
	public HistoryListAdaptor(Context context, int layoutResourceId, List<E> objects) {
		super(context, layoutResourceId, objects);
		this.layoutResourceId = layoutResourceId;
		this.timeResId = R.id.historyKlokkeslet;
		this.dateResId = R.id.historyDato;
		this.tempObjects = objects;
	}

	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {		
		View rowView = convertView;
	    if (rowView == null) {
			LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			rowView = inflater.inflate(layoutResourceId, null);			
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.time = (TextView) rowView.findViewById(timeResId);
			viewHolder.date = (TextView) rowView.findViewById(dateResId);
			rowView.setTag(viewHolder);
	    }

	    ViewHolder holder = (ViewHolder) rowView.getTag();			
		ListData data = (ListData) tempObjects.get(position);
		holder.time.setText(data.getTime());
		holder.date.setText(data.getDate());
		
		return rowView;
	}
}
